package org.jeecg.modules.product.risk.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.CommonAPI;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.product.risk.entity.TbBaseMeasuresTaskRecord;
import org.jeecg.modules.product.risk.service.ITbBaseMeasuresTaskRecordService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.product.until.BaseRiskCommonConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

/**
 * @Description: 风险排查记录
 * @Author: jeecg-boot
 * @Date:   2024-04-29
 * @Version: V1.0
 */
@Tag(name="风险排查记录")
@RestController
@RequestMapping("/TbBaseMeasuresTaskRecord/tbBaseMeasuresTaskRecord")
@Slf4j
public class TbBaseMeasuresTaskRecordController extends JeecgController<TbBaseMeasuresTaskRecord, ITbBaseMeasuresTaskRecordService> {
	@Autowired
	private ITbBaseMeasuresTaskRecordService tbBaseMeasuresTaskRecordService;
	@Autowired
	private CommonAPI commonAPI;

	/**
	 * 分页列表查询
	 *
	 * @param tbBaseMeasuresTaskRecord
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "风险排查记录-分页列表查询")
	@Operation(summary="风险排查记录-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<TbBaseMeasuresTaskRecord>> queryPageList(TbBaseMeasuresTaskRecord tbBaseMeasuresTaskRecord,
																 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
																 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
																 HttpServletRequest req) {
		QueryWrapper<TbBaseMeasuresTaskRecord> queryWrapper = QueryGenerator.initQueryWrapper(tbBaseMeasuresTaskRecord, req.getParameterMap());
		Page<TbBaseMeasuresTaskRecord> page = new Page<TbBaseMeasuresTaskRecord>(pageNo, pageSize);
		queryWrapper.eq("deleted",CommonConstant.DEL_FLAG_0.toString());
		IPage<TbBaseMeasuresTaskRecord> pageList = tbBaseMeasuresTaskRecordService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 * 分页列表查询我自己的排查任务
	 *
	 * @param tbBaseMeasuresTaskRecord
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "风险排查记录-分页列表查询我自己的排查任务")
	@Operation(summary="风险排查记录-分页列表查询我自己的排查任务")
	@GetMapping(value = "/myTaskRecordList")
	public Result<IPage<TbBaseMeasuresTaskRecord>> queryPageMyTaskRecordList(TbBaseMeasuresTaskRecord tbBaseMeasuresTaskRecord,
																			 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
																			 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
																			 HttpServletRequest req) {
		QueryWrapper<TbBaseMeasuresTaskRecord> queryWrapper = QueryGenerator.initQueryWrapper(tbBaseMeasuresTaskRecord, req.getParameterMap());
		Page<TbBaseMeasuresTaskRecord> page = new Page<TbBaseMeasuresTaskRecord>(pageNo, pageSize);
		//获取当前用户
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		queryWrapper.like("identify_people", sysUser.getUsername());
		queryWrapper.eq("deleted", CommonConstant.DEL_FLAG_0.toString());
		IPage<TbBaseMeasuresTaskRecord> pageList = tbBaseMeasuresTaskRecordService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	@RequestMapping(value = "/listCompositeCondition", method = RequestMethod.GET)
	public Result<Page<TbBaseMeasuresTaskRecord>> queryPageListBylistCompositeCondition(TbBaseMeasuresTaskRecord tbBaseMeasuresTaskRecord,
																						@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
																						@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
																						@RequestParam(name="nodeType") String nodeType,
																						@RequestParam(name="checkStatus") String checkStatus,
																						HttpServletRequest req) {
		Result<Page<TbBaseMeasuresTaskRecord>> result = new Result<Page<TbBaseMeasuresTaskRecord>>();
		Page<TbBaseMeasuresTaskRecord> pageList = new Page<TbBaseMeasuresTaskRecord>(pageNo,pageSize);
		pageList = tbBaseMeasuresTaskRecordService.queryPageListBylistCompositeCondition(pageList,checkStatus,nodeType);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}


	/**
	 * 包保责任任务记录分页列表查询
	 *
	 * @param tbBaseMeasuresTaskRecord
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "风险排查记录-包保责任任务记录分页列表查询")
	@Operation(summary="风险排查记录-包保责任任务记录分页列表查询")
	@GetMapping(value = "/guaranteeLiabilityList")
	public Result<IPage<TbBaseMeasuresTaskRecord>> queryPageGuaranteeLiabilityList(TbBaseMeasuresTaskRecord tbBaseMeasuresTaskRecord,
																				   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
																				   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
																				   HttpServletRequest req) {
		QueryWrapper<TbBaseMeasuresTaskRecord> queryWrapper = QueryGenerator.initQueryWrapper(tbBaseMeasuresTaskRecord, req.getParameterMap());
		Page<TbBaseMeasuresTaskRecord> page = new Page<TbBaseMeasuresTaskRecord>(pageNo, pageSize);
		queryWrapper.eq("deleted",CommonConstant.DEL_FLAG_0.toString());
		queryWrapper.eq("is_defend", BaseRiskCommonConstant.RISK_GUARANTEELIABILITY.toString());
		IPage<TbBaseMeasuresTaskRecord> pageList = tbBaseMeasuresTaskRecordService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param tbBaseMeasuresTaskRecord
	 * @return
	 */
	@AutoLog(value = "风险排查记录-添加")
	@Operation(summary="风险排查记录-添加")
	@RequiresPermissions("TbBaseMeasuresTaskRecord:tb_base_measures_task_record:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody TbBaseMeasuresTaskRecord tbBaseMeasuresTaskRecord) {
		tbBaseMeasuresTaskRecord.setDeleted(CommonConstant.DEL_FLAG_0.toString());
		tbBaseMeasuresTaskRecordService.save(tbBaseMeasuresTaskRecord);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param tbBaseMeasuresTaskRecord
	 * @return
	 */
	@AutoLog(value = "风险排查记录-编辑")
	@Operation(summary="风险排查记录-编辑")
	@RequiresPermissions("TbBaseMeasuresTaskRecord:tb_base_measures_task_record:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody TbBaseMeasuresTaskRecord tbBaseMeasuresTaskRecord) {
		tbBaseMeasuresTaskRecordService.updateById(tbBaseMeasuresTaskRecord);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "风险排查记录-通过id删除")
	@Operation(summary="风险排查记录-通过id删除")
	@RequiresPermissions("TbBaseMeasuresTaskRecord:tb_base_measures_task_record:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		tbBaseMeasuresTaskRecordService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "风险排查记录-批量删除")
	@Operation(summary="风险排查记录-批量删除")
	@RequiresPermissions("TbBaseMeasuresTaskRecord:tb_base_measures_task_record:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.tbBaseMeasuresTaskRecordService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "风险排查记录-通过id查询")
	@Operation(summary="风险排查记录-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<TbBaseMeasuresTaskRecord> queryById(@RequestParam(name="id",required=true) String id) {
		TbBaseMeasuresTaskRecord tbBaseMeasuresTaskRecord = tbBaseMeasuresTaskRecordService.getById(id);
		if(tbBaseMeasuresTaskRecord==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(tbBaseMeasuresTaskRecord);
	}

	/**
	 * 导出excel
	 *
	 * @param request
	 * @param tbBaseMeasuresTaskRecord
	 */
	@RequiresPermissions("TbBaseMeasuresTaskRecord:tb_base_measures_task_record:exportXls")
	@RequestMapping(value = "/exportXls")
	public ModelAndView exportXls(HttpServletRequest request, TbBaseMeasuresTaskRecord tbBaseMeasuresTaskRecord) {
		return super.exportXls(request, tbBaseMeasuresTaskRecord, TbBaseMeasuresTaskRecord.class, "风险排查记录");
	}

	/**
	 * 通过excel导入数据
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequiresPermissions("TbBaseMeasuresTaskRecord:tb_base_measures_task_record:importExcel")
	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		return super.importExcel(request, response, TbBaseMeasuresTaskRecord.class);
	}

	/**
	 *  风险任务排查
	 *
	 * @param tbBaseMeasuresTaskRecord
	 * @return
	 */
	@AutoLog(value = "风险排查记录-风险任务排查")
	@Operation(summary="风险排查记录-风险任务排查")
	@RequestMapping(value = "/editStatus", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> editStatus(@RequestBody TbBaseMeasuresTaskRecord tbBaseMeasuresTaskRecord) {
		UpdateWrapper<TbBaseMeasuresTaskRecord> updateWrapper =new UpdateWrapper<>();
		updateWrapper.set("id",tbBaseMeasuresTaskRecord.getId());
		updateWrapper.set("check_status",tbBaseMeasuresTaskRecord.getCheckStatus());
		updateWrapper.set("check_time",tbBaseMeasuresTaskRecord.getCheckTime());
		tbBaseMeasuresTaskRecordService.updateById(tbBaseMeasuresTaskRecord);
		return Result.OK("任务排查完成!");
	}

	/**
	 * 本周个人风险排查任务情况查询
	 * @param username
	 * @return
	 */
	//@AutoLog(value = "风险排查记录-分页列表查询")
	@Operation(summary="风险排查记录-本周个人排查情况查询")
	@GetMapping(value = "/countForThisWeekTaskRecordByUsername")
	public Result<List<Map>> countForThisWeekTaskRecordByUsername(@RequestParam(name="username") String username) {
		return Result.OK(tbBaseMeasuresTaskRecordService.countForThisWeekTaskRecordByUsername(username));
	}

	/**
	 * 本企业上周，今日，本周风险排查任务情况查询
	 * @param nodeType
	 * @return
	 */
	@AutoLog(value = "风险排查记录-本企业上周，今日，本周风险排查任务情况查询")
	@Operation(summary="风险排查记录-本企业上周，今日，本周风险排查任务情况查询")
	@GetMapping(value = "/countForTimeFrame")
	public Result<?> countForTimeFrame(@RequestParam(name="nodeType") String nodeType) {
		return Result.OK(tbBaseMeasuresTaskRecordService.countForTimeFrame(nodeType));
	}

	/**
	 * 本月累计统计
	 * @return
	 */
	@AutoLog(value = "风险排查记录-本月累计统计")
	@Operation(summary="风险排查记录-本月累计统计")
	@GetMapping(value = "/countForThisMonthTaskRecord")
	public Result<?> countForThisMonthTaskRecord() {
		return Result.OK(tbBaseMeasuresTaskRecordService.countForThisMonthTaskRecord());
	}
}
